#include <QFileDialog>
#include "Common.h"
#include "Include/Settings.h"
#include "ui_Common.h"
#include "ClassCollector.h"

Common::Common(QWidget *parent) :
    ISettingsView(parent),
    ui(new Ui::Common)
{
    ui->setupUi(this);

    connect(ui->chkOpenLastProject, SIGNAL(toggled(bool)), SLOT(onRestoreToggled(bool)));
    connect(ui->btnRebuildCache, SIGNAL(clicked()), SLOT(onRebuildCache()));
    connect(ui->btnSelectPy3, SIGNAL(clicked()), SLOT(onSelectPy3()));
    connect(ui->btnSelectUic, SIGNAL(clicked()), SLOT(onSelectPyUic4()));

    ui->chkOpenLastProject->setChecked(Plugins::Settings::instance()->restoreProject());
    ui->chkRestoreFiles->setChecked(Plugins::Settings::instance()->restoreFiles());
    ui->edtPy3Path->setText(Plugins::Settings::instance()->py3());
    ui->edtPyUic4->setText(Plugins::Settings::instance()->pyuic4());
}

Common::~Common()
{
    delete ui;
}

QString Common::displayName()
{
    return tr("Common");
}

QIcon Common::icon()
{
    return QIcon::fromTheme("preferences-system");
}

void Common::onRestoreToggled(bool toggled)
{
    ui->chkRestoreFiles->setEnabled(toggled);
}

void Common::onRebuildCache()
{
    ClassCollector dial;
    dial.exec();
}

void Common::onSelectPy3()
{
    QString exec = QFileDialog::getOpenFileName(this, tr("Select python3 interpreter"));
    if (!exec.isEmpty()){
        ui->edtPy3Path->setText(exec);
    }
}

void Common::onSelectPyUic4()
{
    QString exec = QFileDialog::getOpenFileName(this, tr("Select pyuic4 program"));
    if (!exec.isEmpty()){
        ui->edtPyUic4->setText(exec);
    }
}

void Common::save()
{
    Plugins::Settings::instance()->setRestoreProject(ui->chkOpenLastProject->isChecked());
    Plugins::Settings::instance()->setRestoreFiles(ui->chkRestoreFiles->isChecked());
    Plugins::Settings::instance()->setPy3(ui->edtPy3Path->text());
    Plugins::Settings::instance()->setPyuic4(ui->edtPyUic4->text());
}

